{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Perplexity Search Tool\n",
    "\n",
    "The Perplexity AI search integration allows users to perform real-time web searches within the AG2 framework. Follow these steps to integrate `PerplexitySearchTool` with AG2 Agents.\n",
    "\n",
    "## Configuring Your Perplexity API Key\n",
    "\n",
    "1. **Create a Perplexity Account**:\n",
    "   - Visit [Perplexity AI](https://www.perplexity.ai/)\n",
    "   - Click `Sign Up` and create an account\n",
    "   - Select Free Tier during registration\n",
    "\n",
    "2. **Get API Key**:\n",
    "   - Navigate to [API Settings](https://www.perplexity.ai/settings/api)\n",
    "   - Add payment method (required)\n",
    "   - Generate API key under **API Keys**\n",
    "\n",
    "3. **Set Environment Variable:**\n",
    "   ```bash\n",
    "   export PERPLEXITY_API_KEY=\"your_api_key_here\"\n",
    "   ```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Package Installation\n",
    "\n",
    "To get started with the `Perplexity Search` integration in AG2, follow these steps:\n",
    "\n",
    "Install AG2 with `openai` extra since we use openai in our examples:\n",
    "```bash\n",
    "pip install -U \"ag2[openai]\"\n",
    "```\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:\n",
    "> ```bash\n",
    "> pip install -U \"autogen[openai]\"\n",
    "> ```\n",
    "> or\n",
    "> ```bash\n",
    "> pip install -U \"ag2[openai]\"\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.\n",
    "\n",
    "\n",
    "You're all set! Now you can start using Perplexity AI Search with AG2."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Implementation\n",
    "The `PerplexitySearchTool` enables agents to perform real time AI Powered web search."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from autogen import AssistantAgent, UserProxyAgent\n",
    "from autogen.tools.experimental import PerplexitySearchTool"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Agent Configuration\n",
    "\n",
    "Configure an assistant agent and user proxy to be used for LLM recommendation and execution respectively.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"AUTOGEN_USE_DOCKER\"] = \"False\"\n",
    "\n",
    "from autogen import LLMConfig\n",
    "\n",
    "config_list = LLMConfig(config_list={\"api_type\": \"openai\", \"model\": \"gpt-5-nano\"})\n",
    "\n",
    "assistant = AssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    llm_config=config_list,\n",
    ")\n",
    "\n",
    "user_proxy = UserProxyAgent(name=\"user_proxy\", human_input_mode=\"NEVER\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tool Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "perplexity_search_tool = PerplexitySearchTool(api_key=os.getenv(\"PERPLEXITY_API_KEY\"), max_tokens=1000)\n",
    "\n",
    "# Register the tool for LLM recommendation and execution.\n",
    "perplexity_search_tool.register_for_llm(assistant)\n",
    "perplexity_search_tool.register_for_execution(user_proxy)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start the Conversation\n",
    "\n",
    "With the setup complete, you can now use the assistant to fetch live web search results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = user_proxy.initiate_chat(\n",
    "    recipient=assistant,\n",
    "    message=\"What happened with stock prices after deepseek was launched? please search the web.\",\n",
    "    max_turns=2,\n",
    ")\n",
    "print(f\"Final Answer: {response.summary}\")"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Perplexity Search Integration with AG2",
   "tags": [
    "tools",
    "perplexity",
    "web-search",
    "search"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
